package admin.data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TreeMap;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import models.CategoryModel;
import models.RouteModel;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

public class RouteData {
	private Objectify ofy;
	
	private Cache cache = null;
	
	private String prefix = "Route_";
	
	public RouteData() {
		try {
			ObjectifyService.register(RouteModel.class);
		} catch (Exception e) {
			//e.printStackTrace();
		}
		
		try {
            cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
        } catch (CacheException e) {
           //e.printStackTrace();
        }
		
        ObjectifyOpts opts = new ObjectifyOpts().setSessionCache(true);
		ofy = ObjectifyService.begin(opts);
	}
	
	public RouteModel getRoute(String id) {
		RouteModel route = new RouteModel();		
		try {
			route = ofy.get(new Key<RouteModel>(RouteModel.class, id));
		} catch (Exception e) {
			route = null;
		}		
		return route;
	}
	
	public void insert(RouteModel route) {
		ofy.put(route);
	}  
	
	public void delete(RouteModel route) {		
		ofy.delete(route);
	}
	
	public List<RouteModel> getListRoute()
	{
		List<RouteModel> list = new ArrayList<RouteModel>();
		Query<RouteModel> query = ofy.query(RouteModel.class);
		for (RouteModel route : query) {
			list.add(route);
		}
		return list;
	}
	
	public TreeMap<String, RouteModel> getMapRoute()
	{
		TreeMap<String, RouteModel> list = new TreeMap<String, RouteModel>();
		Query<RouteModel> query = ofy.query(RouteModel.class);
		for (RouteModel route : query) {
			list.put(route.getId(), route);
		}
		return list;
	}
}
